<?php
	require_once ("util.php");
	require_once ("sql.php");
	require_once ("role.php");
	require_once ("userrolemapping.php");

	class ManageUserRole{
		
		public function userRoleListAll(){
			$sql= SQLQuery::USER_ROLE_LIST_ALL;
			$util = new DBUtil ();
			$conn = $util->getConnection ();
			$stmt = $conn->prepare ( $sql );
			$status = $stmt->execute ();
			$userrolemappings = array ();
			$result = $stmt->get_result ();
			if ($result->num_rows > 0) {
				while ( $row = $result->fetch_array () ) {
					$userrolemapping = new UserRoleMapping ();
					$userrolemapping->setUserId ( $row ["UID"] );
					$userrolemapping->setUserName ( $row ["USERNAME"] );
					$userrolemapping->setRoleId ( $row ["RID"] );
					$userrolemapping->setRoleName ( $row ["ROLE"] );
					array_push ( $userrolemappings, $userrolemapping );
				}
			}
			$conn->close ();
			return $userrolemappings;
		}
		public function userRoleListAllSelected($userId){
			$sql= SQLQuery::USER_ROLE_CHECKED;
			$util = new DBUtil ();
			$conn = $util->getConnection ();
			$stmt = $conn->prepare ( $sql );
			$stmt->bind_param('d',$userId);
			$status = $stmt->execute ();
			$roles = array ();
			$result = $stmt->get_result ();
			if ($result->num_rows > 0) {
				while ( $row = $result->fetch_array () ) {
					$role = new Role ();
					$role->setId ( $row ["ID"] );
					$role->setRole ( $row ["ROLE"] );
					$role->setDescription ( $row ["DESCRIPTION"] );
					$role->setChecked ( $row ["CHECKED"] );
					array_push ( $roles, $role );
				}
			}
			$conn->close ();
			return $roles;
		}
		
		public function userRoleUpdate($userId,$roleId){
			$util = new DBUtil ();
			$conn = $util->getConnection ();
			$sql= SQLQuery::USER_ROLE_UPDATE;
			$stmt = $conn->prepare ( $sql );
			$stmt->bind_param('dd',$roleId,$userId);
			$status = $stmt->execute ();
			$stmt->close();
			$conn->close ();
			return $status ;
				
		}
		public function userRoleRemove($userId){
			$sql= SQLQuery::USER_ROLE_REMOVE;
			$util = new DBUtil ();
			$conn = $util->getConnection ();
			$stmt = $conn->prepare ( $sql );
			$stmt->bind_param('d',$userId);
			$status = $stmt->execute ();
			$stmt->close();
			$conn->close ();
			return $status;
		}
	}
?>